// joinUs.js
const HOST = require('../../config.js').host;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isApplySuccess:false,
    iptClass:'',
    iptClass1: '',
    openid:'',
    isChecked:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取用户openid
    // wx.login({
    //   success: function (loginCode) {
    //     var appid = 'wxdd60d653577f7018'; //填写微信小程序appid  
    //     var secret = ''; //填写微信小程序secret  

    //     //调用request请求api转换登录凭证  
    //     wx.request({
    //       url: 'https://api.weixin.qq.com/sns/jscode2session?appid=‘+<code></code>appid+’&secret=‘+secret+’&grant_type=authorization_code&js_code=' + loginCode.code,
    //       header: {
    //         'content-type': 'application/json'
    //       },
    //       success: function (res) {
    //         console.log(res.data.openid) //获取openid  
    //       }
    //     })
    //   }
    // });  
    // this.setData({
    //   openid: app.userId
    // })
  },
  userInfoSubmit:function(e) {
    let name = e.detail.value.name.replace(/\s/ig,'');
    let phone = e.detail.value.phone.replace(/\s/g, "");
    let that = this;
    if (!name) {
      wx.showToast({
        title: '姓名不能为空！',
        image: '../../assets/images/error.png',
        mask: true        
      });
      this.setData({
        iptClass:'erroClass'
      })
      return false;
    } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
      wx.showToast({
        title: '电话号码输入不正确！',
        image: '../../assets/images/error.png',
        mask: true        
      })
      this.setData({
        iptClass1: 'erroClass'
      })
      return false;
    }
    //提交注册请求
    wx.request({
      url: HOST + 'v1/joininfo/saveJoinInfo',
      data:{
        memberName:name,
        memberMobile:phone
      },
      method:'POST',
      success:function(res) {
        if (res.data.sysCode === 1) {
          wx.showToast({
            title: '提交成功！',
            icon: 'success',
            mask:true
          })
          setTimeout(function () {
            that.setData({
              isApplySuccess: true
            })
          }, 800)
        } else if (res.data.sysCode === 2) { //判断是否注册过
          wx.showToast({
            title: '该手机号码已经申请成功，请更换手机重试！',
            icon: 'success',
            mask: true
          })
        }
        
      },
      fail:function(res) {
        if (res.data.sysCode === 0) {
          wx.showToast({
            title: '请检查网络是否正常...',
            icon: 'loading',
            mask: true
            
          })
        }
      }
    })
  },
  removeErroClass:function(e) {
    if (e.currentTarget.id == "name") {
      this.setData({
        iptClass: ''
      })
    } else {
      this.setData({
        iptClass1: ''
      })
    }
    
  },
  directToReadme:function() {
    wx.navigateTo({
      url: '../mustRead/mustRead'
    })
  },
  checkboxChange:function(e) {
    if (e.detail.value[0] == "false") {
      this.setData({
        isChecked:true
      })
    } else {
      this.setData({
        isChecked: false
      })
    }
  }
})